package com.qyc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qyc.dto.AddOrUpDateDiseaseRecordDTO;
import com.qyc.enums.CodeMsgEnum;
import com.qyc.exception.SystemException;
import com.qyc.pojo.DiseaseRecord;
import com.qyc.service.DiseaseRecordService;
import com.qyc.mapper.DiseaseRecordMapper;
import com.qyc.utils.BeanCopyUtils;
import com.qyc.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author qinyc
* @description 针对表【disease_record(疾病记录表)】的数据库操作Service实现
* @createDate 2023-06-26 12:14:34
*/
@Service
public class DiseaseRecordServiceImpl extends ServiceImpl<DiseaseRecordMapper, DiseaseRecord>
    implements DiseaseRecordService{

    //注入病症记录Mapper层对象
    @Autowired
    private DiseaseRecordMapper diseaseRecordMapper;

    /**
     * @Author qinyc
     * @Description  新增或更新病症记录
     * @version: v1.0
     * @Date 13:25 2023/6/26
     **/
    @Override
    public ResponseResult saveOrUpdate(AddOrUpDateDiseaseRecordDTO addOrUpDateDiseaseRecordDTO) {
        /*
         * 参数校验
         */
        //bean拷贝
        DiseaseRecord diseaseRecord = BeanCopyUtils.copyBean(addOrUpDateDiseaseRecordDTO, DiseaseRecord.class);
        if (Objects.isNull(addOrUpDateDiseaseRecordDTO.getDrId())){
            //如果没有病症记录ID  新增
            diseaseRecordMapper.insert(diseaseRecord);
            //返回统一响应
            return ResponseResult.success();
        }else {
            //如果有病症记录ID  修改
            diseaseRecordMapper.updateById(diseaseRecord);
            //返回统一响应
            return ResponseResult.success();
        }
    }

    /**
     * @Author qinyc
     * @Description  根据记录ID删除病症记录
     * @version: v1.0
     * @Date 13:32 2023/6/26
     **/
    @Override
    public ResponseResult deleteById(Integer drId) {
        //参数校验
        if (Objects.isNull(drId)){
            //病症ID为空
            throw new SystemException(CodeMsgEnum.DISEASE_RECORD_ID_IS_NOT_NULL);
        }else{
            DiseaseRecord diseaseRecord = diseaseRecordMapper.selectById(drId);
            if (Objects.isNull(diseaseRecord)){
                //如果根据ID查询不到指定病症记录  指定病症记录不存在
                throw new SystemException(CodeMsgEnum.DISEASE_RECORD_IS_NOT_EXIST);
            }
            //存在指定ID的病症记录  进行删除操作
            diseaseRecordMapper.deleteById(drId);
            //返回成功响应
            return ResponseResult.success();
        }
    }

    /**
     * @Author qinyc
     * @Description  批量删除病症记录
     * @version: v1.0
     * @Date 13:43 2023/6/26
     **/
    @Override
    public ResponseResult deleteByIdAll(List<Integer> drIds) {
        /*
         * 参数校验
         */
        //参数校验完毕
        //执行批量删除操作
        diseaseRecordMapper.deleteBatchIds(drIds);
        //返回成功响应
        return ResponseResult.success();
    }
}




